<?php
session_start();

if (!isset($_SESSION['username'])) {
    echo json_encode(['status' => 'error', 'message' => '未登录']);
    exit();
}

$currentUser = $_SESSION['username'];
$groupId = $_POST['group_id'] ?? null;

if (!$groupId) {
    echo json_encode(['status' => 'error', 'message' => '群聊ID不能为空']);
    exit();
}

$userChatFile = __DIR__ . "/user/$currentUser/chats.json";

if (!file_exists($userChatFile)) {
    echo json_encode(['status' => 'error', 'message' => '用户聊天文件不存在']);
    exit();
}

$chats = json_decode(file_get_contents($userChatFile), true) ?: [];

// 过滤掉当前群聊
$updatedChats = array_filter($chats, function($chat) use ($groupId) {
    return !($chat['type'] === 'group' && $chat['id'] === $groupId);
});

// 保存更新后的聊天列表
file_put_contents($userChatFile, json_encode(array_values($updatedChats)));

echo json_encode(['status' => 'success']);
?>